package com.huawei.esdk.tp.device.terminal;

import org.apache.log4j.Logger;

import com.huawei.esdk.platform.common.SDKErrorCode;
import com.huawei.esdk.platform.common.bean.commu.BaseJsonBean;
import com.huawei.esdk.platform.common.config.ConfigManager;
import com.huawei.esdk.platform.exception.ProtocolAdapterException;
import com.huawei.esdk.platform.nemgr.itf.IDeviceConnection;
import com.huawei.esdk.tp.bean.terminal.ReqRecordBroadBean;
import com.huawei.esdk.tp.bean.terminal.ResBean;

public class TE30V1R1C01Device extends TerminalDevice implements
        TE30V1R1C01ServiceItf
{
    private static final Logger LOGGER = Logger
            .getLogger(TE30V1R1C01Device.class);

    public TE30V1R1C01Device(String sap)
    {
        super(sap);
    }

    @Override
    public IDeviceConnection createConnection(String appId, String sap,
            String user, String pwd)
    {
        return new TE30V1R1C01Connection(protocolAdapter, this, user, pwd);
    }
    
    @Override
    public SDKErrorCode startRecordBroad()
    {
        return recordBroadAPI(ConfigManager.getInstance()
                .getValue("professional.common.start_record"));
    }

    @Override
    public SDKErrorCode stopRecordBroad()
    {
        return recordBroadAPI(ConfigManager.getInstance()
                .getValue("professional.common.stop_record"));
    }
    
    @Override
    public SDKErrorCode startDirectBroad()
    {
        return recordBroadAPI(ConfigManager.getInstance()
                .getValue("professional.common.start_direct"));
    }
    
    @Override
    public SDKErrorCode stopDirectBroad()
    {
        return recordBroadAPI(ConfigManager.getInstance()
                .getValue("professional.common.stop_direct"));
    }
    
    protected SDKErrorCode recordBroadAPI(String action)
    {
        ReqRecordBroadBean reqJsonBean = new ReqRecordBroadBean();
        SDKErrorCode result = new SDKErrorCode();
        reqJsonBean.setAction(action);
        ResBean res = null;
        try
        {
            res = (ResBean) protocolAdapter.syncSendMessage(reqJsonBean,
                    "WEB_RecordBroadAPI", BaseJsonBean.class.getName());
            errorCodeDecode(res, result);
            return result;
        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("startRecordBroad method failed", e);
            result.setErrCode(e.getErrorCode());
            return result;
        }
    }
}
